package channel

import (
	"context"

	model0 "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/user/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type EWalletModel struct {
	ctx context.Context
	db  *gorm.DB
	logx.Logger
}

func (m *EWalletModel) getDB() *gorm.DB {
	return m.db
}

func NewEWalletModel(ctx context.Context, db *gorm.DB) *EWalletModel {
	return &EWalletModel{
		ctx:    ctx,
		db:     db,
		Logger: logx.WithContext(ctx),
	}
}

func (m *EWalletModel) UserEWalletsDel(in *rpc.UserEWalletsDelRequest) (*rpc.UserEWalletsDelResponse, error) {
	del := m.db.Where("id = ?", in.Id)

	tx := del.Delete(&model.UserChannelEwallet{
		Model: model0.Model{
			ID: (in.Id),
		},
		UserId: in.UserId,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrDel
	}

	return &rpc.UserEWalletsDelResponse{
		IsDel: true,
	}, nil
}

// ------------------user channel ewallet ------------------
func (m *EWalletModel) UserEWalletsInfo(in *rpc.UserEWalletsRequest) (*rpc.UserEWalletsResponse, error) {
	var userEWalletInfos []*model.UserChannelEwallet
	var userEWalletsInfoList []*rpc.UserEWalletsInfo

	// 查询所有用户银行信息
	query := m.db
	if in.UserId != 0 {
		query = query.Where("user_id = ?", in.UserId)
	}
	if in.Username != "" {
		query = query.Where("username LIKE ?", "%"+in.Username+"%")
	}
	if in.WalletName != "" {
		query = query.Where("name = ?", in.WalletName)
	}
	if in.Account != "" {
		query = query.Where("account LIKE ?", "%"+in.Account+"%")
	}

	query.Find(&userEWalletInfos)
	if len(userEWalletInfos) == 0 {
		return &rpc.UserEWalletsResponse{
			UserEWalletsInfoList: []*rpc.UserEWalletsInfo{},
		}, nil
	}
	for _, userEWallet := range userEWalletInfos {
		userEWalletInfo := &rpc.UserEWalletsInfo{
			Id:         userEWallet.ID,
			UserId:     userEWallet.UserId,
			WalletName: userEWallet.Name,
			Username:   userEWallet.Username,
			Account:    userEWallet.Account,
		}
		userEWalletsInfoList = append(userEWalletsInfoList, userEWalletInfo)
	}

	return &rpc.UserEWalletsResponse{
		UserEWalletsInfoList: userEWalletsInfoList,
	}, nil
}

func (m *EWalletModel) UserEWalletsInsert(in *rpc.UserEWalletsInsertRequest) (*rpc.UserEWalletsInsertResponse, error) {
	tx := m.db.Create(&model.UserChannelEwallet{
		UserId:   in.UserId,
		Account:  in.Account,
		Name:     in.Name,
		Username: in.Username,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrInsert
	}

	return &rpc.UserEWalletsInsertResponse{
		IsInsert: true,
	}, nil
}

func (m *EWalletModel) UserEWalletsPage(in *rpc.UserEWalletsPageRequest) (*rpc.UserEWalletsPageResponse, error) {
	var userChannelEwalletInfos []*model.UserChannelEwallet
	offset := in.Page.PageSize * (in.Page.PageNo - 1)

	// 查询所有用户银行信息
	query := m.db.Model(&model.UserChannelEwallet{})
	if in.Param.UserId != 0 {
		query = query.Where("user_id = ?", in.Param.UserId)
	}
	if in.Param.Username != "" {
		query = query.Where("username LIKE ?", "%"+in.Param.Username+"%")
	}
	if in.Param.Account != "" {
		query = query.Where("account LIKE ?", "%"+in.Param.Account+"%")
	}
	if in.Param.WalletName != "" {
		query = query.Where("name LIKE ?", "%"+in.Param.WalletName+"%")
	}

	var total int64
	_ = query.Count(&total).Error
	var userBankInfoList []*rpc.UserEWalletsInfo

	query = query.Limit(int(in.Page.PageSize)).Offset(int(offset))
	query.Find(&userChannelEwalletInfos)

	for _, userEWallet := range userChannelEwalletInfos {
		userEWalletInfo := &rpc.UserEWalletsInfo{
			Id:         userEWallet.ID,
			UserId:     userEWallet.UserId,
			WalletName: userEWallet.Name,
			Username:   userEWallet.Username,
			Account:    userEWallet.Account,
		}
		userBankInfoList = append(userBankInfoList, userEWalletInfo)
	}
	return &rpc.UserEWalletsPageResponse{
		DataList: userBankInfoList,
		Total:    total,
	}, nil
}

func (m *EWalletModel) UserEWalletsUpdate(in *rpc.UserEWalletsUpdateRequest) (*rpc.UserEWalletsUpdateResponse, error) {
	tx := m.db.Where("id = ?", in.Id).Updates(&model.UserChannelEwallet{
		UserId:   in.UserId,
		Username: in.Username,
		Account:  in.Account,
		Name:     in.Name,
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		return nil, xerrors.ErrUpdate
	}

	return &rpc.UserEWalletsUpdateResponse{
		IsUpdate: true,
	}, nil
}
